<?php

require_once('database.php');
$bodyString = "";
if (!$parentId) { $parentId = $_GET['id']; }

if ($_SERVER['REQUEST_METHOD']=="POST") {
	$id = $_POST['id'];

	$linkedChild = $_POST['linkedChild'];

	if ($linkedChild == 'best_news') {
		if ($id) {
		$sort_order = htmlentities($_POST["sort_order"],ENT_QUOTES);
		$timestamp = htmlentities($_POST["timestamp"],ENT_QUOTES);
		$title = htmlentities($_POST["title"],ENT_QUOTES);
		$publishDate = htmlentities($_POST["publishDate"],ENT_QUOTES);
		$body = htmlentities($_POST["body"],ENT_QUOTES);

			$SQLedit = "UPDATE best_news SET ";	
			$SQLedit .= "  sort_order = '$sort_order',  timestamp = FROM_UNIXTIME($timestamp),  title = '$title',  publishDate = '$publishDate',  body = '$body'  "; 
			$SQLedit .= " WHERE id = '$id' ";
			$SQLeditResult = $db->query($SQLedit);
		}
		else {
		$sort_order = htmlentities($_POST["sort_order"],ENT_QUOTES);
		$timestamp = htmlentities($_POST["timestamp"],ENT_QUOTES);
		$title = htmlentities($_POST["title"],ENT_QUOTES);
		$publishDate = htmlentities($_POST["publishDate"],ENT_QUOTES);
		$body = htmlentities($_POST["body"],ENT_QUOTES);

			$SQLnew = "INSERT INTO best_news (sort_order,timestamp,title,publishDate,body) VALUES ('$sort_order',CURDATE(), '$title','$publishDate','$body')";
			$SQLnewResult = $db->query($SQLnewResult);
			$newChildId = mysql_insert_id();
			$SQLnewAssociation = "INSERT INTO $linkTableName (parent_id,child_id) VALUES ($parentId,$newChildId)";
			$SQLnewAssociationResult = $db->query($SQLnewAssociation);
		}
	}
}

if (!$linkTableName) {
	$linkTableName = $_GET['linkTableName'];
	$childTableName = $_GET['childTableName'];
	$parentId = $_GET['parentId'];
}

$SQLlist = "SELECT * FROM $linkTableName LEFT JOIN $childTableName ON $childTableName".".id = $linkTableName".".child_id WHERE $linkTableName".".parent_id = $parentId ORDER BY $linkTableName".".sort_order ASC";
//echo $SQLlist;
//$SQLlist = "SELECT * FROM best_news WHERE $parentTable_SQL_where";
$SQLlistResult = $db->query($SQLlist);
while( $rowArray = $db->fetch_array($SQLlistResult) ) {		$sort_order = $rowArray["sort_order"];
		$timestamp = $rowArray["timestamp"];
		$title = $rowArray["title"];
		$publishDate = $rowArray["publishDate"];
		$body = $rowArray["body"];
		$id = $rowArray["id"];
	//Being if/else (id)
	if($id) { 
		$bodyString .= "\n<div class='backListRecord best_news_record' id='$id'>";
		$bodyString .= "\n  <div class='backListElement'><div class='dragIcon'></div></div>";
		$bodyString .= "\n  <div class='backListElement'>Date and Time Published : ".strftime('%m',strtotime($timestamp))."-".strftime('%d',strtotime($timestamp))."-".strftime('%Y',strtotime($timestamp))."</div>";
		$bodyString .= "\n  <div class='backListElement backListTitle'>$title</div>";
		$bodyString .= "\n  <div class='backListElement'>Date / Month Published: $publishDate</div>";
						
		$bodyString .= "\n<div class='listPageButtons'><a class='editBtn positiveBtns' href='best_news_write.php?id=$id&sessionDepth=".($_GET['sessionDepth']+1)."&linkedChild=".best_news."'>EDIT</a><a class='removeBtn negativeBtns'>REMOVE</a>";
		$bodyString .= "</div></div>";	
	}	else {
		$SQLlinkCheck = "DELETE $linkTableName.* FROM $linkTableName LEFT JOIN $childTableName ON $childTableName".".id = $linkTableName".".child_id WHERE $childTableName".".id IS NULL";
		$SQLlinkExecute = $db->query($SQLlinkCheck);
	} //end if/else(id)
} //end while ($rowArray)
?>
	<script type='text/javascript'>
	var jsTableName = 'best_news';
	$(function() {
		jQuery.fileDelete = function(id,field) {
		$.ajax({
			url: "ajax.php",
			data: "tablename="+jsTableName+"&id="+id+"&field="+field+"&AJAXtask=deleteFile",
			cache: false,
			complete: function(){
				//alert("tablename="+jsTableName+"&id="+id+"&field="+field+"&AJAXtask=deleteFile");
			}
		});
	};	

	jQuery.deleteAllFiles = function(id,field) {
				
		};		
	});
	</script>
	
	<?php echo $bodyString; ?>
	

